package jp.ac.aiit.yc.sww.messaging.services;

import static jp.ac.aiit.yc.sww.constant.Constants.*;
import flex.messaging.FlexContext;
import flex.messaging.MessageClient;
import flex.messaging.messages.Message;
import flex.messaging.services.MessageService;
import flex.messaging.services.ServiceAdapter;
import java.util.*;

import jp.ac.aiit.yc.sww.constant.*;
import jp.ac.aiit.yc.sww.database.*;
import jp.ac.aiit.yc.sww.database.dao.*;
import jp.ac.aiit.yc.sww.database.dto.*;
import jp.ac.aiit.yc.sww.messaging.*;
import jp.ac.aiit.yc.sww.util.Util;

public class SwwServiceAdapter extends ServiceAdapter {

	private PageStatus pageStatus;
	/*
	public SwwServiceAdapter(){
		System.out.println(new Date().toString() + " SimpleCustomAdapter constructed.");
		MessageClient.addMessageClientCreatedListener(new ClientWatcher());
		this.databaseManager = new DatabaseManager();
	}
	*/

	@Override
    public void start() {
    	super.start();
    	pageStatus = new PageStatus();
		System.out.println((new Date()).toString() + " SwwServiceAdapter start.");
		MessageClient.addMessageClientCreatedListener(new ClientWatcher());
    }

	@Override
    public void stop() {
    	super.stop();
		System.out.println((new Date()).toString() + " SwwServiceAdapter stop.");
	}


	@Override
    public Object invoke(Message message) {
		System.out.println((new Date()).toString() + " " + message.getHeader(HEADER_USER) + " invoke SwwServiceAdapter.");

		// DBのtry-catch
		// データの保存の結果にかかわらず、メッセージは送信する
		try {
			System.out.println((new Date()).toString() + " message.getHeader(HEADER_OPERATION)=" + message.getHeader(HEADER_OPERATION));

			// page
			if(HEADER_PAGE.equals(message.getHeader(HEADER_OPERATION))){
				System.out.println((new Date()).toString() + " message.getHeader(HEADER_OPERATION)=" + message.getHeader(HEADER_OPERATION));

				int pageID =(Integer)(message.getBody());
				pageStatus.setPageID(pageID);
				FlexContext.getServletContext().setAttribute(SESSION_PAGESTATUS, pageStatus);
				System.out.println((new Date()).toString() + " SwwServiceAdapter pageID=" + pageStatus.getPageID());
				//Added by Kato 2009/02/07 ----start
				PagePresentDto pagePre = new PagePresentDto();	//全部ｈｅａｄｅｒ情報なので、必要ないがとりあえず作成
				DatabaseManager dm = new DatabaseManager();
				PagePresentDao dao = new PagePresentDao();
				dao.setHeaders(message.getHeaders());
				boolean result = dm.register(pagePre, dao);
				//Added by Kato 2009/02/07 ----end
				

			} else if(HEADER_MODE.equals(message.getHeader(HEADER_OPERATION))){
				// Mode処理を登録
				System.out.println((new Date()).toString() + " message.getHeader(HEADER_OPERATION)=" + message.getHeader(HEADER_OPERATION));
				System.out.println((new Date()).toString() + " message.getHeader(HEADER_USER)=" + message.getHeader(HEADER_USER));
				System.out.println((new Date()).toString() + " message.getHeader(HEADER_ROOM)=" + message.getHeader(HEADER_ROOM));
				System.out.println((new Date()).toString() + " message.getHeader(HEADER_PAGE)=" + message.getHeader(HEADER_PAGE));
				
				ModeDto mode = new ModeDto(); //全部ｈｅａｄｅｒ情報なので、必要ないがとりあえず作成
				DatabaseManager dm = new DatabaseManager();
				ModeDao dao = new ModeDao();
				dao.setHeaders(message.getHeaders());
				boolean result = dm.register(mode, dao);
				System.out.println((new Date()).toString() + " dm.register(mode)=" + result);
			} else if(HEADER_DRAW.equals(message.getHeader(HEADER_OPERATION))){
				// 描画処理を登録
				DrawDto draw = (DrawDto)message.getBody();
				System.out.println((new Date()).toString() + " draw.getColor()=" + draw.getColor());
				System.out.println((new Date()).toString() + " draw.getThickness()=" + draw.getThickness());
				System.out.println((new Date()).toString() + " draw.getPoints()=" + draw.getPoints());

				DatabaseManager dm = new DatabaseManager();
				DrawDao dao = new DrawDao();
				dao.setHeaders(message.getHeaders());
				boolean result = dm.register(draw, dao);
				System.out.println((new Date()).toString() + " dm.register(draw)=" + result);
			} else if(HEADER_CLEAR_TRAIL.equals(message.getHeader(HEADER_OPERATION))){
				// クリア処理を登録
				System.out.println((new Date()).toString() + " message.getHeader(HEADER_OPERATION)=" + message.getHeader(HEADER_OPERATION));
				System.out.println((new Date()).toString() + " message.getHeader(HEADER_USER)=" + message.getHeader(HEADER_USER));
				System.out.println((new Date()).toString() + " message.getHeader(HEADER_ROOM)=" + message.getHeader(HEADER_ROOM));
				System.out.println((new Date()).toString() + " message.getHeader(HEADER_PAGE)=" + message.getHeader(HEADER_PAGE));
				
				ClearDto clear = new ClearDto(); 
				DatabaseManager dm = new DatabaseManager();
				ClearDao dao = new ClearDao();
				dao.setHeaders(message.getHeaders());
				boolean result = dm.register(clear, dao);
				System.out.println((new Date()).toString() + " dm.register(clear)=" + result);
			
			} else if(HEADER_NOTE.equals(message.getHeader(HEADER_OPERATION))){
				NoteDataDto note = new NoteDataDto();
				note = (NoteDataDto)message.getBody();
				DatabaseManager dm = new DatabaseManager();
				NoteDataDao dao = new NoteDataDao();
				dao.setHeaders(message.getHeaders());
				boolean result = dm.register(note, dao);
			}
		} catch (Exception e) {
			// TODO エラーの処理方法は要検討
			e.printStackTrace();
		}

        // メッセージを他クライアントに送信する
        MessageService msgService = (MessageService)getDestination().getService();
        msgService.pushMessageToClients(message, true);
	    return null;
    }

}